﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigEaterCh.Kontrolki;

namespace BigEaterCh.Formularze
{
  public partial class FormEditUserPosilek : Form
  {
    private EnumPosilek _typPosilku;
    private DateTime _data;
    private string posil;

    public FormEditUserPosilek(EnumPosilek typPosilku, DateTime data)
    {
      _typPosilku = typPosilku;
      _data = data;
      InitializeComponent();
      posil = _typPosilku.ToString();
      RefreshData();
    }

    void RefreshData()
    {
      FormBigEaterCh.GetDataBase();
      if( FormBigEaterCh.Ze != null )
      {

        var dict = FormBigEaterCh.Ze.Potrawy.Select(t => new
        {
          t.GrupaPotraw,
          t.IdPotrawy,
          t.NazwaPotrawy,
          t.Lekka
        }).GroupBy(t => t.GrupaPotraw).OrderBy(t => t.Key);


        var query = from c in FormBigEaterCh.Ze.DniPotrawy
                    where ( c.Data == _data && c.IdPosilku == posil )
                    select new
                    {
                      c.IdPotrawy
                    };

        var q1 = query.ToDictionary(t => t.IdPotrawy);

        foreach( var employee in dict )
        {
          var lvg = new ListViewGroup(employee.Key);
          listView1.Groups.Add(lvg);
          foreach( var empl in employee )
          {
            var lvi = new ListViewItem(empl.NazwaPotrawy);
            lvi.SubItems.Add(Convert.ToBoolean(empl.Lekka).ToString());
            lvi.Tag = empl.IdPotrawy;
            lvi.Group = lvg;
            if(q1.ContainsKey(empl.IdPotrawy))
            {
              lvi.Checked = true;
            }
            listView1.Items.Add(lvi);
          }
        } 
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      Dictionary<int,int> dict = listView1.CheckedItems.Cast<ListViewItem>().ToDictionary(it => (int) it.Tag, it => 0);
      var query = from c in FormBigEaterCh.Ze.DniPotrawy
                  where (c.Data == _data && c.IdPosilku == posil)
                  select c;

      var query1 = from c in FormBigEaterCh.Ze.DniPotrawy
                    where ( c.Data == _data && c.IdPosilku == posil )
                    select new
                    {
                      c.IdPotrawy
                    };

      var q1 = query1.ToDictionary(t => t.IdPotrawy);
      
      foreach(var o in query)
      {
        if(!dict.ContainsKey(o.IdPotrawy))
          FormBigEaterCh.Ze.DniPotrawy.DeleteObject(o);      
      }

      foreach( var o in dict )
      {
        if( !q1.ContainsKey(o.Key) )
        {
          var dp = FormBigEaterCh.Ze.DniPotrawy.CreateObject();
          dp.Data = _data;
          dp.IdPosilku = _typPosilku.ToString();
          dp.IdPotrawy = o.Key;
          FormBigEaterCh.Ze.AddToDniPotrawy(dp);
        }
      }

      FormBigEaterCh.SaveDataBase();
      FormBigEaterCh.Ze.Refresh(System.Data.Objects.RefreshMode.StoreWins, FormBigEaterCh.Ze.DniPotrawy);

      Close();
    }




  }
}
